package exception;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ThrowDemo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个大于0的数：");
        int i = sc.nextInt();
//        if (i<0){
//            System.out.println("您输入的数据不合法");
//            return;
//        }
        if (i<0)
            throw new IllegalArgumentException("您输入的数据不正确");
            System.out.println("您输入的数据为："+i);
            //将读入的整数写入到文件
            try{
                doWrite(i);
            }catch (IOException e){
                System.out.println("记录写文件的错误日志");
                throw new RuntimeException("系统暂时错误，请稍后尝试");
            }
    }
    static void doWrite(int i) throws IOException {
        try(FileOutputStream fos = new FileOutputStream("./fos.txt")){
            fos.write(i);
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("读写文件失败");
            throw e;
        }
    }
}